package javaday02;

import java.util.Random;
import java.util.Scanner;

/*
 * 走迷宫
 */
public class Homework3MIGONG {
	public static void main(String[] args){
		int choose=1;
		int[][] MAZE;
		while(true){
			MAZE=getMaze();
			printMaze(MAZE,new int[]{0,0});
			System.out.println("重新开始？1是/0否");
			choose=new Scanner(System.in).nextInt();
			if(choose==1)
				continue;
			break;
		}
		walkMaze(MAZE);
	}
	/*
	 * 获取迷宫
	 */
	public static int[][] getMaze(){
		Random r=new Random();
		int[][] MAZE = new int[5][5];
		for(int i=0;i<5;i++){
			for(int j=0;j<5;j++){
				if(r.nextInt(2)==0)
					MAZE[i][j]=1;
			}
		}
		MAZE[0][0]=MAZE[4][4]=0;
		return MAZE;
	}

	/*
	 * 走迷宫
	 */
	public static void walkMaze(int[][] maze){
		Scanner s=new Scanner(System.in);
		//记录当前位置
		int[] now={0,0};
		//记录之前数据
		int[] before={0,0};
		printMaze(maze,now);
		while (!(now[0]==4&&now[1]==4)){
			before[0]=now[0];
			before[1]=now[1];
			System.out.println("走");
			switch(s.next().charAt(0)){
			case 'w':
				if(now[0]>0&&maze[now[0]-1][now[1]]!=1)
					now[0]--;
				break;
			case 'a':
				if(now[1]>0&&maze[now[0]][now[1]-1]!=1)
					now[1]--;
				break;
			case 's':
				if(now[0]<4&&maze[now[0]+1][now[1]]!=1)
					now[0]++;
				break;
			case 'd':
				if(now[1]<4&&maze[now[0]][now[1]+1]!=1)
					now[1]++;
				break;
			}
			maze[before[0]][before[1]]=0;
			//打印迷宫
			printMaze(maze,now);
		}
		System.out.println("通关");
	}
	/*
	 * 打印迷宫
	 */
	public static void printMaze(int[][] maze,int[] now){
		for(int i=0;i<5;i++){
			for(int j=0;j<5;j++){
				if(i==now[0]&&j==now[1])
					System.out.print("♥ ");
				else
					System.out.print(maze[i][j]+" ");}
			System.out.println();
	}
	}
}
